//! 因为trait类似interface，所以trait可以当成泛型类来使用，即代表一类实现了该trait的struct

trait HasSound {
    fn get_sound(&self) -> String {
        "Default sound".to_string()
    }
}

struct Cat {
    sound: String,
}

impl HasSound for Cat {
    fn get_sound(&self) -> String {
        self.sound.clone()
    }
}

struct Bell {
    sound: String,
}

impl HasSound for Bell {
    fn get_sound(&self) -> String {
        self.sound.clone()
    }
}

fn make_sound<T: HasSound>(t: &T) {
    println!("{}!", t.get_sound());
}

pub fn test() {
    let kitty = Cat { sound: "Meow".to_string() };
    let the_bell = Bell { sound: "Ding Dong".to_string() };

    make_sound(&kitty);
    make_sound(&the_bell);
}